package com.qingmi.farmer.window;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.qingmi.farmer.BuildConfig;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.DataSetting;
import com.qingmi.farmer.data.model.UpdateVersion;
import com.wjjun.base.data.preference.UserPreference;
import com.wjjun.customview.window.BaseDialog;
import com.wjjun.utils.AppUtils;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import zlc.season.rxdownload.RxDownload;
import zlc.season.rxdownload.entity.DownloadStatus;

/**
 * 警告对话框
 * Created by 67535 on 2018/5/22.
 */
public class UpdateVersionDialog extends BaseDialog {

    private TextView msgTextView;
    private ProgressBar progressBar;
    private View optionsView;

    //最低版本号
    private long minVerNo;

    //最新的版本号
    private long newVerNo;
    //新版本 版本名
    private String VerName;
    //新版本说明
    private String verDesc;
    //下载地址
    private String url;

    private UserPreference userPreference;


    public UpdateVersionDialog(@NonNull Context context, UserPreference userPreference, UpdateVersion updateVersion) {
        super(context);
        this.userPreference = userPreference;
        minVerNo = updateVersion.getMinVerNo();
        newVerNo = updateVersion.getNowVerNo();

        VerName = updateVersion.getNowVerName();
        verDesc = updateVersion.getNowVerDesc();
        if (!TextUtils.isEmpty(updateVersion.getNowVerUrl()) && updateVersion.getNowVerUrl().startsWith("http")) {
            url = updateVersion.getNowVerUrl();
        } else {
            url = DataSetting.getPicUrl(updateVersion.getNowVerUrl());
        }

        if (TextUtils.isEmpty(VerName)) {
            setTitle(getString(R.string.format_new_version, ""));
        } else {
            setTitle(getString(R.string.format_new_version, VerName));
        }

        if (BuildConfig.VERSION_CODE < minVerNo) {  //需要强制更新
            setCancelable(false);
            cancelView.setVisibility(View.GONE);
        } else {
            setCancelable(true);
        }
        msgTextView.setText(verDesc);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.dialog_update_version;
    }

    @Override
    protected void initViews() {
        super.initViews();

        msgTextView = findViewById(R.id.tv_message);
        progressBar = findViewById(R.id.progress);
        optionsView = findViewById(R.id.ly_options);

    }

    @Override
    protected void initContent() {
        super.initContent();
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onCancel() {
        userPreference.setIgnoreVersion(newVerNo);
        dismiss();
    }

    @Override
    protected void onConfirm() {
        msgTextView.setVisibility(View.GONE);
        progressBar.setVisibility(View.VISIBLE);
        optionsView.setVisibility(View.GONE);
        RxDownload.getInstance().maxRetryCount(3)
                .context(getContext())
                .autoInstall(true)
                .download(url, "qingmi.apk", AppUtils.getCachePath(getContext()))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DownloadStatus>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast(R.string.error_download_fail);
                        msgTextView.setVisibility(View.VISIBLE);
                        progressBar.setVisibility(View.GONE);
                        optionsView.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onNext(final DownloadStatus status) {
                        double progress = ((double) status.getDownloadSize() / (double) status.getTotalSize()) * 100.0;
                        progressBar.setProgress((int) progress);
                    }
                });

    }
}
